package shortlink.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import shortlink.common.convention.result.Result;
import shortlink.common.convention.result.Results;
import shortlink.remote.ShortLinkRemoteService;
import shortlink.remote.dto.req.ShortLinkBatchCreateReqDTO;
import shortlink.remote.dto.req.ShortLinkCreateReqDTO;
import shortlink.remote.dto.req.ShortLinkPageReqDTO;
import shortlink.remote.dto.req.ShortLinkUpdateReqDTO;
import shortlink.remote.dto.resp.*;
import shortlink.toolkit.EasyExcelWebUtil;

import java.util.List;

@RestController
@RequiredArgsConstructor
public class ShortLinkController {

    private final ShortLinkRemoteService shortLinkRemoteService;
    /*创建短链接*/
    @PostMapping("/api/short-link/admin/link/create")
    public Result<ShortLinkCreateRespDTO> createShortLink(@RequestBody ShortLinkCreateReqDTO requestParam) {
        return shortLinkRemoteService.createShortLink(requestParam) ;
    }
    /*批量创建短链接*/
    @PostMapping("/api/short-link/admin/link/Batchcreate")
    public void batchCreateShortLink(@RequestBody ShortLinkBatchCreateReqDTO requestParam, HttpServletResponse response) {
        Result<ShortLinkBatchCreateRespDTO> shortLinkBatchCreateRespDTOResult = shortLinkRemoteService.batchCreateShortLink(requestParam);
        if (shortLinkBatchCreateRespDTOResult.isSuccess()) {
            List<ShortLinkBaseInfoRespDTO> baseLinkInfos = shortLinkBatchCreateRespDTOResult.getData().getBaseLinkInfos();
            EasyExcelWebUtil.write(response, "批量创建短链接-SaaS短链接系统", ShortLinkBaseInfoRespDTO.class, baseLinkInfos);
        }
    }
    /*分页查询短链接*/
    @GetMapping("/api/short-link/admin/link/pageselect")
    public Result<Page<ShortLinkPageRespDTO>> pageShortlink(ShortLinkPageReqDTO requestParam) {
        return shortLinkRemoteService.pageShortLink(requestParam.getGid(),requestParam.getOrderTag(),requestParam.getCurrent(),requestParam.getSize());

    }
    /*修改短链接信息*/
    @PostMapping("/api/short-link/admin/link/update")
    public Result<Void> updateShortLink(@RequestBody ShortLinkUpdateReqDTO requestParam) {
        shortLinkRemoteService.updateShortLink(requestParam);
        return  Results.success();
    }
    /*查询短链接数量*/
    @GetMapping("/api/short-link/admin/link/groupselect")
    public Result<List<ShortLinkGroupCountQueryRespDTO>> listGroupShortLinkCount(@RequestParam("requestParam") List<String> requestParam) {
        return shortLinkRemoteService.listGroupShortLinkCount(requestParam);
    }
}
